/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.androidpn.client;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.esun.RabbitApplication;
import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.util.log.EDebug;

/**
 * Broadcast receiver that handles push notification messages from the server. This should be registered as receiver in AndroidManifest.xml.
 * 
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public final class NotificationReceiver extends BroadcastReceiver {

	private static final String LOGTAG = "NotificationReceiver";

	// private NotificationService notificationService;

	public NotificationReceiver() {
	}

	// public NotificationReceiver(NotificationService notificationService) {
	// this.notificationService = notificationService;
	// }

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d(LOGTAG, "NotificationReceiver.onReceive()...");

		String action = intent.getAction();
		SharedPreferences sharedpre = context.getSharedPreferences(Config.CLIENT_PREFERENCES, Context.MODE_PRIVATE);
		boolean isMasterPushSwitchOpened = sharedpre.getBoolean(Key.KEY_PARENT_CHECKBOX_PREFERENCE, true);
		String notificationId = intent.getStringExtra(Constants.NOTIFICATION_ID);
		String notificationApiKey = intent.getStringExtra(Constants.NOTIFICATION_API_KEY);
		String notificationTitle = intent.getStringExtra(Constants.NOTIFICATION_TITLE);
		String notificationMessage = intent.getStringExtra(Constants.NOTIFICATION_MESSAGE);
		String notificationUri = intent.getStringExtra(Constants.NOTIFICATION_URI);

		EDebug.o(this, "action = " + action);
		EDebug.o(this, "master push switch state = " + isMasterPushSwitchOpened);
		EDebug.o(this, "title = " + notificationTitle);
		EDebug.o(this, "message = " + notificationMessage);
		EDebug.o(this, "notification id  = " + notificationId);
		EDebug.o(this, "api key = " + notificationApiKey);
		EDebug.o(this, "uri = " + notificationUri);

		if (Constants.ACTION_SHOW_NOTIFICATION.equals(action) && isMasterPushSwitchOpened) {

			if (TextUtils.isEmpty(notificationMessage)) {
				return;
			}
			String[] messages = notificationUri.split("#");
			if (messages.length != 3) {
				return;
			}

			for (int i = 0; i < messages.length; i++) {
				EDebug.o(this, "message index " + i + "=" + messages[i]);
			}

			// Log.d(LOGTAG, "notificationId=" + notificationId);
			// Log.d(LOGTAG, "notificationApiKey=" + notificationApiKey);
			// Log.d(LOGTAG, "notificationTitle=" + notificationTitle);
			// Log.d(LOGTAG, "notificationMessage=" + notificationMessage);
			// Log.d(LOGTAG, "notificationUri=" + notificationUri);

			RabbitApplication application = (RabbitApplication) context.getApplicationContext();
			if (application.isSetNotificationEnable()) {
				setNotificationInApplication(application, messages);
			}

			boolean isNotifyUsers = false;
			// 61=表示彩票客户端1.6版本信息 70=表示即时比分客户端信息
			if ("61".equals(messages[2])) {
				// 1开奖信息推送
				if ("1".equals(messages[1])) {
					isNotifyUsers = true;
					// 2激活注册推送
				} else if ("2".equals(messages[1])) {
					isNotifyUsers = true;
					// 3活动运营推送
				} else if ("3".equals(messages[1])) {
					isNotifyUsers = true;
					// 4 即时比分定制推送
				} else if ("4".equals(messages[1])) {
					// 其他
				} else {

				}
			}

			if (isNotifyUsers) {
				Notifier notifier = new Notifier(context);
				notifier.notify(notificationId, notificationApiKey, notificationTitle, notificationMessage, notificationUri);
			}
		}

		// } else if (Constants.ACTION_NOTIFICATION_CLICKED.equals(action)) {
		// String notificationId = intent
		// .getStringExtra(Constants.NOTIFICATION_ID);
		// String notificationApiKey = intent
		// .getStringExtra(Constants.NOTIFICATION_API_KEY);
		// String notificationTitle = intent
		// .getStringExtra(Constants.NOTIFICATION_TITLE);
		// String notificationMessage = intent
		// .getStringExtra(Constants.NOTIFICATION_MESSAGE);
		// String notificationUri = intent
		// .getStringExtra(Constants.NOTIFICATION_URI);
		//
		// Log.e(LOGTAG, "notificationId=" + notificationId);
		// Log.e(LOGTAG, "notificationApiKey=" + notificationApiKey);
		// Log.e(LOGTAG, "notificationTitle=" + notificationTitle);
		// Log.e(LOGTAG, "notificationMessage=" + notificationMessage);
		// Log.e(LOGTAG, "notificationUri=" + notificationUri);
		//
		// Intent detailsIntent = new Intent();
		// detailsIntent.setClass(context, NotificationDetailsActivity.class);
		// detailsIntent.putExtras(intent.getExtras());
		// // detailsIntent.putExtra(Constants.NOTIFICATION_ID, notificationId);
		// // detailsIntent.putExtra(Constants.NOTIFICATION_API_KEY, notificationApiKey);
		// // detailsIntent.putExtra(Constants.NOTIFICATION_TITLE, notificationTitle);
		// // detailsIntent.putExtra(Constants.NOTIFICATION_MESSAGE, notificationMessage);
		// // detailsIntent.putExtra(Constants.NOTIFICATION_URI, notificationUri);
		// detailsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// detailsIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
		//
		// try {
		// context.startActivity(detailsIntent);
		// } catch (ActivityNotFoundException e) {
		// Toast toast = Toast.makeText(context,
		// "No app found to handle this request",
		// Toast.LENGTH_LONG);
		// toast.show();
		// }
		//
		// } else if (Constants.ACTION_NOTIFICATION_CLEARED.equals(action)) {
		// //
		// }

	}

	/**
	 * @param application
	 * @param messages
	 * @title setNotificationInApplication
	 * @description 记录push种类
	 */
	private void setNotificationInApplication(RabbitApplication application, String[] messages) {

		if ("61".equals(messages[2])) {
			if ("1".equals(messages[1])) {
				application.setReceivedLotteryNotification(true);
			} else if ("2".equals(messages[1])) {
				application.setReceivedRegisteNotification(true);
			} else if ("3".equals(messages[1])) {
				application.setReceivedActivityNotification(true);
			}
		}

	}

}
